package com.rmy.queue.common.factory;

import com.rmy.CompUtils;
import com.rmy.common.utils.LogUtils;
import com.rmy.common.utils.ip.AddressUtils;
import com.rmy.domain.CompArea;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rmy.BerrUtils;
import rmy.VoicUtils;
import rmy.domain.BerrArea;
import rmy.domain.VoicCmd;

import java.util.List;
import java.util.TimerTask;

/**
 * 设备调用异步工厂（产生任务用）
 *
 */
public class DeviceAsyncFactory
{
    private static final Logger rmy_queue_logger = LoggerFactory.getLogger("sys-queue");

    /**
     * 异步执行发送COMP显示内容
     *
     * @param strIp IP地址
     * @param nPort 端口
     * @param strParams 参数
     * @param strContent 内容
     * @return 任务task
     */
    public static TimerTask sendComp(final String strIp, final int nPort, final String strParams,
                                     final String strContent)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                String address = AddressUtils.getRealAddressByIP(strIp);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(strIp));
                s.append(address);
                s.append(LogUtils.getBlock(nPort));
                s.append(LogUtils.getBlock(strParams));
                s.append(LogUtils.getBlock(strContent));
                // 打印信息到日志
                rmy_queue_logger.info(s.toString());
                List<CompArea> compAreas = CompUtils.parseContent(strContent);

                try {
                    CompUtils.sendComp(strIp, nPort, strParams, compAreas);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }



    /**
     * 异步执行发送VOIC声音设备
     *
     * @param strIp IP地址
     * @param nPort 端口
     * @param strParams 参数
     * @param strContent 内容
     * @return 任务task
     */
    public static TimerTask sendVoic(final String strIp, final int nPort, final String strParams,
                                     final String strContent)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                String address = AddressUtils.getRealAddressByIP(strIp);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(strIp));
                s.append(address);
                s.append(LogUtils.getBlock(nPort));
                s.append(LogUtils.getBlock(strParams));
                s.append(LogUtils.getBlock(strContent));
                // 打印信息到日志
                rmy_queue_logger.info(s.toString());
                VoicCmd voicCmd = VoicUtils.parseContent(strContent);

                try {
                    VoicUtils.sendVoic(strIp, nPort, strParams, voicCmd);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }


    /**
     * 异步执行发送BERR声音设备
     *
     * @param strIp IP地址
     * @param nPort 端口
     * @param strParams 参数
     * @param strContent 内容
     * @return 任务task
     */
    public static TimerTask sendBerr(final String strIp, final int nPort, final String strParams,
                                     final String strContent)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                String address = AddressUtils.getRealAddressByIP(strIp);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(strIp));
                s.append(address);
                s.append(LogUtils.getBlock(nPort));
                s.append(LogUtils.getBlock(strParams));
                s.append(LogUtils.getBlock(strContent));
                // 打印信息到日志
                rmy_queue_logger.info(s.toString());
                List<BerrArea> berrAreaList = BerrUtils.parseContent(strContent);

                try {
                    BerrUtils.sendBerr(strIp, nPort, strParams, berrAreaList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }

}
